package com.sys.menu.controller;

import com.app.utils.Result;
import com.app.view.TreeMenu;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.core.aspect.Log;
import com.sys.menu.entity.SysMenu;
import com.sys.menu.service.SysMenuService;
import com.sys.menu.view.SysMenuView;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 菜单表 前端控制器
 * </p>
 *
 * @author djianfeng
 * @since 2018-07-24
 */
@Controller
@RequestMapping("/sys/menu")
public class SysMenuController {
    @Autowired
    SysMenuService menuService;

    @Log("查询菜单列表")
    @RequiresPermissions("MenuList")
    @RequestMapping(value = "/list.do", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> list(ServletRequest request, ServletResponse response, Page<SysMenuView> page, SysMenuView view) {
        Map<String, Object> map = new HashMap<>();
        List<SysMenuView> list = menuService.getSysMenuViews(page, view);
        map.put("records", list);
        map.put("total", page.getTotal());
        return map;
    }

    @Log("进入菜单管理")
    @RequiresPermissions("Menu")
    @RequestMapping("/golist.do")
    public String tolist() {
        return "system/menu/list";
    }

    @Log("查询菜单")
    @RequiresPermissions("EditMenu")
    @RequestMapping("/goEdit.do")
    public ModelAndView goEdit(SysMenu entity) {
        if (entity.getId() != null) {
            entity = this.menuService.selectById(entity.getId());
        }
        Map map = new HashMap();
        map.put("entity", entity);
        List<TreeMenu> treeMenus = menuService.selectTreeMenu();
        map.put("treeMenus", treeMenus);
        return new ModelAndView("system/menu/edit", map);
    }

    @Log("保存菜单")
    @RequiresPermissions("EditMenu")
    @RequestMapping(value = "/save.do", method = RequestMethod.POST)
    @ResponseBody
    public Result save(SysMenu entity) {
        boolean n = this.menuService.insertOrUpdate(entity);
        return Result.returnResult(n);
    }

    @Log("删除菜单")
    @RequiresPermissions("DelMenu")
    @RequestMapping(value = "/del.do", method = RequestMethod.POST)
    @ResponseBody
    public Result del(SysMenu entity) {
        boolean n = this.menuService.deleteById(entity.getId());
        return Result.returnResult(n);
    }

    @Log("修改菜单")
    @RequiresPermissions("UpdateMenu")
    @RequestMapping(value = "/update.do", method = RequestMethod.POST)
    @ResponseBody
    public Result update(SysMenu entity) {
        boolean n = this.menuService.updateById(entity);
        return Result.returnResult(n);
    }
}
